package com.example.rabbitmq.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

//直连交换机
@Component
public class EmailConsumer {
    @RabbitListener(queues = "heJun_test_queue")
    public void show(String email, Message message, Channel channel) throws IOException {
        try {
            System.out.println("消费者1拿到消息："+email);
            int i=1/0;
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
            System.out.println("消费者1消息确认" + message.getMessageProperties().getConsumerQueue() + ",接受到了回调方法"+message.getMessageProperties().getDeliveryTag());
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            //对这个消息拒绝，并且重新回归消息队列
            System.out.println("尝试重发：" + message.getMessageProperties().getConsumerQueue());
            // TODO 该消息已经导致异常，重发无意义，应自己实现补偿机制
        }
    }

    @RabbitListener(queues = "heJun_test_queue")
    public void show2(String email){
        System.out.println("消费者2拿到消息："+email);
    }
}
